// pages/house/house.js
// const list =[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    has_police_room: [
      {
        value: 0,
        name: '否',
        selected:false
      },
      {
        value: 1,
        name: '是',
        selected:false
      },
    ],
    has_duty_room: [
      {
        value: 0,
        name: '否',
        selected:false
      },
      {
        value: 1,
        name: '是',
        selected:false
      },
    ],
    close_remark: [
      {
        value: 0,
        name: '全封闭',
        selected:false
      },
      {
        value: 1,
        name: '半封闭',
        selected:false
      },
      {
        value: 2,
        name: '全开放',
        selected:false
      },
    ],
    danger_place: [
      {
        value: 0,
        name: '否',
        selected:false
      },
      {
        value: 1,
        name: '是',
        selected:false
      },
    ],
    e_monitor_install: [
      {
        value: 0,
        name: '否',
        selected:false
      },
      {
        value: 1,
        name: '是',
        selected:false
      },
    ],
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    flag: 0,
    comArr:[],
    gridArr:[],
    newArr:[],
    g_id:'',
    c_id:'',
    id:'',
  },

  onLoad(opts) {
    console.log(opts)
    this.setData({
      id:opts.id
    })
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow(){
    let pages = getCurrentPages(); 
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
    this.loadInfo()
  },

  loadInfo(){
    let that = this
    let access_token = wx.getStorageSync('access_token')
    let id = that.data.id
    wx.request({
      url: 'https://baokang.wshoto.com/wx/basicinfo',
      data:{
        access_token:access_token,
        type:1,
        id:id
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          that.setData({
            listInfo:res.data.info,
          })
          // 警务室
          if(res.data.info.has_police_room == 0){
            that.data.has_police_room[0].selected = true;
            that.data.has_police_room[1].selected = false
            that.setData({
              has_police_room:that.data.has_police_room
            })
          } else {
            that.data.has_police_room[0].selected = false;
            that.data.has_police_room[1].selected = true
            that.setData({
              has_police_room:that.data.has_police_room
            })
          }
          // 值班室
          if(res.data.info.has_duty_room == 0){
            that.data.has_duty_room[0].selected = true;
            that.data.has_duty_room[1].selected = false
            that.setData({
              has_duty_room:that.data.has_duty_room
            })
          } else {
            that.data.has_duty_room[0].selected = false;
            that.data.has_duty_room[1].selected = true
            that.setData({
              has_duty_room:that.data.has_duty_room
            })
          }
          // 电子监控
          if(res.data.info.e_monitor_install == 0){
            that.data.e_monitor_install[0].selected = true;
            that.data.e_monitor_install[1].selected = false
            that.setData({
              e_monitor_install:that.data.e_monitor_install
            })
          } else {
            that.data.e_monitor_install[0].selected = false;
            that.data.e_monitor_install[1].selected = true
            that.setData({
              e_monitor_install:that.data.e_monitor_install
            })
          }
          // 封闭状态
          if(res.data.info.close_remark == 0){
            that.data.close_remark[0].selected = true;
            that.data.close_remark[1].selected = false
            that.data.close_remark[2].selected = false
            that.setData({
              close_remark:that.data.close_remark
            })
          } else if(res.data.info.close_remark == 1){
            that.data.close_remark[0].selected = false;
            that.data.close_remark[1].selected = true
            that.data.close_remark[2].selected = false;
            that.setData({
              close_remark:that.data.close_remark
            })
          } else {
            that.data.close_remark[0].selected = false;
            that.data.close_remark[1].selected = false
            that.data.close_remark[2].selected = true;
            that.setData({
              close_remark:that.data.close_remark
            })
          }
          // 治安乱点
          if(res.data.info.danger_place == 0){
            that.data.danger_place[0].selected = true;
            that.data.danger_place[1].selected = false
            that.setData({
              danger_place:that.data.danger_place
            })
          } else {
            that.data.danger_place[0].selected = false;
            that.data.danger_place[1].selected = true
            that.setData({
              danger_place:that.data.danger_place
            })
          }
        }
        wx.hideLoading()
      }
    })
  },

  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    let data = {
      multiIndex:this.data.multiIndex,
      multiObjArray:this.data.multiObjArray
    }
    data.multiIndex[e.detail.column] = e.detail.value
    switch(e.detail.column){
      case 0:
        for(let i in this.data.comArr){
          if(data.multiIndex[0] == i){
            data.multiObjArray[1]=this.data.newArr[i]
          }
        }
        data.multiIndex[1] = 0;
        break
    }
    this.setData(data)
  },

  bindMultiPickerChange(e){
    console.log(e)
    let comIdx = e.detail.value[0]
    let multiIndex = e.detail.value[1]
    let c_id = this.data.rawData[comIdx].c_id
    let g_id = this.data.gridArr[comIdx][multiIndex].g_id
    this.setData({
      multiIndex:e.detail.value,
      g_id:g_id,
      c_id:c_id,
      flag:1
    })
  },

  policeChange(e) {
    console.log(e)
    let item1 = this.data.has_police_room
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  // policeChange(e){
  //   console.log(e)
  // },

  dutyChange(e) {
    let item1 = this.data.has_duty_room
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  eChange(e) {
    let item1 = this.data.e_monitor_install
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  remarkChange(e) {
    let item1 = this.data.close_remark
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  dangerChange(e) {
    let item1 = this.data.danger_place
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  formSubmit(e) {
    console.log(e)
    let { h_name, households_num, resident_num, building_num, carport_num, active_place, fit_machine, defire_machine, desteal_machine, clean_machine_remark, advertise_machine_remark, cleaner, has_police_room, has_duty_room, e_monitor_install, close_remark, danger_place, property_company, property_company_master, contact_tel, protecter_num } = e.detail.value
    let access_token = wx.getStorageSync('access_token')
    // let {c_id,g_id} = this.data
    // if(this.data.c_id == '' || this.data.g_id == ''){
    //   wx.showToast({
    //     title: '所属社区不能为空！',
    //     icon:'none'
    //   })
    //   return false
    // }
    if(e.detail.value.h_name == ''){
      wx.showToast({
        title: '小区名称不能为空！',
        icon:'none'
      })
      return false
    }
    if(e.detail.value.building_num == ''){
      wx.showToast({
        title: '楼栋数不能为空！',
        icon:'none'
      })
      return false
    }
    if(e.detail.value.property_company == ''){
      wx.showToast({
        title: '物业公司名称不能为空！',
        icon:'none'
      })
      return false
    }
    if(e.detail.value.property_company_master == ''){
      wx.showToast({
        title: '物业公司负责人不能为空！',
        icon:'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    if(this.data.id){
      wx.request({
        url: 'https://baokang.wshoto.com/wx/houseadd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          id:this.data.id,
          access_token: access_token,
          h_name: h_name,
          households_num: households_num,
          resident_num: resident_num,
          building_num: building_num,
          carport_num: carport_num,
          active_place: active_place,
          fit_machine: fit_machine,
          defire_machine: defire_machine,
          desteal_machine: desteal_machine,
          clean_machine_remark: clean_machine_remark,
          advertise_machine_remark: advertise_machine_remark,
          cleaner: cleaner,
          has_police_room: has_police_room,
          has_duty_room: has_duty_room,
          e_monitor_install: e_monitor_install,
          close_remark: close_remark,
          danger_place: danger_place,
          property_company: property_company,
          property_company_master: property_company_master,
          contact_tel: contact_tel,
          protecter_num: protecter_num
        },
        success: (res) => {
          wx.hideLoading()
          if(res.data.code == 1){
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    } else {
      wx.request({
        url: 'https://baokang.wshoto.com/wx/houseadd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          access_token: access_token,
          h_name: h_name,
          households_num: households_num,
          resident_num: resident_num,
          building_num: building_num,
          carport_num: carport_num,
          active_place: active_place,
          fit_machine: fit_machine,
          defire_machine: defire_machine,
          desteal_machine: desteal_machine,
          clean_machine_remark: clean_machine_remark,
          advertise_machine_remark: advertise_machine_remark,
          cleaner: cleaner,
          has_police_room: has_police_room,
          has_duty_room: has_duty_room,
          e_monitor_install: e_monitor_install,
          close_remark: close_remark,
          danger_place: danger_place,
          property_company: property_company,
          property_company_master: property_company_master,
          contact_tel: contact_tel,
          protecter_num: protecter_num
        },
        success: (res) => {
          wx.hideLoading()
          if(res.data.code == 1){
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    }
    
  }
})